/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.component;

import com.funcom.peeler.peelable.*;
import com.funcom.peeler.symbol.Symbol;
import com.jmex.bui.BComponent;
import com.jmex.bui.BLabel;
import com.jmex.bui.Rule;
import org.w3c.dom.Element;

import java.util.HashMap;
import java.util.Map;

public class PeelComponentFactory {

    public static PeelComponent makePeelComponent(Symbol.BType type) {

        PeelComponent component = null;

        if ( type == null ) return null;

        switch (type) {

            case BUTTON:
                component = new PeelButton();
                break;
            case CANVASWINDOW:
                component = new PeelCanvasWindow();
                break;
            case CHECKBOX:
                component = new PeelCheckBox();
                break;
            case COMBOBOX:
                component = new PeelComboBox();
                break;
            case CONTAINER:
                component = new PeelContainer();
                break;
            case GEOMVIEW:
                component = new PeelGeomView();
                break;
            case LABEL:
                component = new PeelLabel();
                break;
            case PLACEHOLDER:
                component = new PeelPlaceHolder();
                break;
            case SCROLLPANE:
                component = new PeelContainer();
                break;
            case SLIDER:
                component = new PeelComponent();
                break;
            case TEXTAREA:
                component = new PeelTextArea();
                break;
            case TEXTFIELD:
                component = new PeelTextField();
                break;
            case TOGGLEBUTTON:
                component = new PeelToggleButton();
                break;
            case WORKSPACE:
                component = new PeelWorkSpace();
            default:
                break;
        }

        if ( component != null && type != null )
            component.getComponent().setProperty("Type", type );

        if ( type != Symbol.BType.CANVASWINDOW && type != Symbol.BType.WORKSPACE ) {
            component.getComponent().setName("untitled component");
        }

        return component;
    }

    public static PeelComponent copyComponent( PeelComponent component ) {

        BComponent bComponent = component.getComponent();
        String styleClass = bComponent.getStyleClass();

        Symbol.BType elementType = (Symbol.BType) component.getComponent().getProperty("Type");
        PeelComponent newComponent = makePeelComponent(elementType);

        HashMap< String, Rule > styleSheetProp  = component.getStyleSheet().properties;

        for ( Map.Entry< String, Rule > aStyle : styleSheetProp.entrySet() ) {

            if (aStyle.getValue().styleClass.equals(styleClass)) {

                String fqClass = aStyle.getValue().styleClass;

                if (aStyle.getValue().pseudoClass != null) fqClass += ":" + aStyle.getValue().pseudoClass;

                newComponent.getComponent().setProperty( fqClass, aStyle.getValue() );
            }
        }

        newComponent.getComponent().setSize(bComponent.getWidth(), bComponent.getHeight());
        newComponent.getComponent().setLocation( bComponent.getX(), bComponent.getY() );
        newComponent.getComponent().setAlpha( bComponent.getAlpha() );
        newComponent.getComponent().setStyleClass(bComponent.getStyleClass());

        if (component instanceof PeelLabel) {
            ((BLabel)newComponent.getComponent()).setText(((BLabel) component.getComponent()).getText());
        }

        if (component instanceof PeelContainer) {
            PeelContainer container = (PeelContainer)component;

            int numberOfChildren = container.getComponentCount();

            for (int i = 0; i < numberOfChildren; i++) {

                PeelComponent childComponent = container.getChild(i);
                PeelComponent newChild = copyComponent( childComponent );

                ((PeelContainer)newComponent ).addChild(newChild);
            }
        }
        return newComponent;
    }

    public static PeelComponent makePeelComponent( Element element ) {

        return makePeelComponent( typeFromString( element.getTagName() ));
    }

    private static Symbol.BType typeFromString( String name ) {

        return Symbol.StringToType.get( name );
    }
}


